package com.edu.controller;


import com.edu.dao.NewsDao;
import com.edu.data.News;
import com.edu.data.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/News")
public class NewsController {
    @Autowired
    NewsDao dao;

    @RequestMapping("/list")
    public String list(Model model) {
        List<News> newList=dao.getlist();
        model.addAttribute( "newList" ,newList);
        return "../../manage/news";
    }
    @RequestMapping("/select")
    public String select(Model model,int id) throws IOException, SQLException {
        News news=dao.select(id);
        model.addAttribute("news",news);
        return "../../news-view";
    }
    @RequestMapping("/read")
    public String toModify(Model model,int id) {
        News news=dao.select(id);
        model.addAttribute("news",news);
        return "../../manage/news-updata";
    }
    @RequestMapping("/update")
    public String toModifyDetail(News news)  {
        dao.update(news);
        return "redirect:list";
    }
    @RequestMapping("/add")
    public String toinsert() {
        return "../../manage/news-modify";
    }
    @RequestMapping("/save")
    public String save(News news) throws IOException, SQLException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date= df.format(new Date());
        news.setEnCreateTime(date);
        dao.add(news);
        return "redirect:list";
    }
    @RequestMapping("/delete")
    public String delect(int id) throws IOException, SQLException {
        dao.delect(id);
        return "redirect:list";
    }

}
